package com.atcper

import scala.Console.println
import scala.collection.mutable.ArrayBuffer

/**
 * @author : ranzlupup
 * @date : 2023/2/22 08:39
 */
object Scala08_function_hell2 {
    def main(args: Array[String]): Unit = {

        def fun(int: Int, string: String, char: Char): Boolean = {
            if (int == 0 && string == "" && char == '0') false else true
        }

        println(fun(10, "", '0'))


        // (1)map 映射
        def map(arr: Array[Int], op: Int => Int) = {
            for (elem <- arr) yield op(elem)
        }

        val arr = map(Array(1, 2, 3, 4), (x: Int) => {
            x * x
        })
        println(arr.mkString(","))

        // (2)filter过滤
        def filter(array: Array[Int], op: Int => Boolean) = {
            var arr1: ArrayBuffer[Int] = ArrayBuffer[Int]()
            for (elem <- array if op(elem)) {
                arr1.append(elem)
            }
            arr1.toArray
        }

        var arr1 = filter(Array(1, 2, 3, 4), (x: Int) => if (x == 3) false else true)
        var arr2 = filter(Array(1, 2, 3, 4, 5, 6), (x: Int) => {
            if (x % 2 == 0) {
                true
            } else {
                false
            }
        })

        var arr3 = filter(Array(1, 2, 3, 4, 5, 6), _ % 2 == 0)
        println(arr1.mkString(","))
        println(arr2.mkString(","))
        println(arr3.mkString(","))

        // (3)reduce聚合。有多个参数，且每个参数再后面只使用一次，
        // 则参数省略且后面参数用_表示，第 n 个_代表第 n 个参数
        def reduce(arr: Array[Int], op: (Int, Int) => Int) = {
            var init: Int = arr(0)

            for (elem <- 1 until arr.length) {
                init = op(init, elem)
            }
            init
        }

        val arr4 = reduce(Array(1, 2, 3, 4), (x, y) => x * y)
        println(arr4)
    }
}
